home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / remote.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  34.0 KB  |  1,358 lines

  1. /* Remote target communications for serial-line targets in custom GDB protocol
  2.    Copyright 1988, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Remote communication protocol.
  21.  
  22.    A debug packet whose contents are <data>
  23.    is encapsulated for transmission in the form:
  24.  
  25.     $ <data> # CSUM1 CSUM2
  26.  
  27.     <data> must be ASCII alphanumeric and cannot include characters
  28.     '$' or '#'
  29.  
  30.     CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
  31.     checksum of <data>, the most significant nibble is sent first.
  32.     the hex digits 0-9,a-f are used.
  33.  
  34.    Receiver responds with:
  35.  
  36.     +    - if CSUM is correct and ready for next packet
  37.     -    - if CSUM is incorrect
  38.  
  39.    <data> is as follows:
  40.    All values are encoded in ascii hex digits.
  41.  
  42.     Request        Packet
  43.  
  44.     read registers  g
  45.     reply        XX....X        Each byte of register data
  46.                     is described by two hex digits.
  47.                     Registers are in the internal order
  48.                     for GDB, and the bytes in a register
  49.                     are in the same order the machine uses.
  50.             or ENN        for an error.
  51.  
  52.     write regs    GXX..XX        Each byte of register data
  53.                     is described by two hex digits.
  54.     reply        OK        for success
  55.             ENN        for an error
  56.  
  57.     read mem    mAA..AA,LLLL    AA..AA is address, LLLL is length.
  58.     reply        XX..XX        XX..XX is mem contents
  59.                     Can be fewer bytes than requested
  60.                     if able to read only part of the data.
  61.             or ENN        NN is errno
  62.  
  63.     write mem    MAA..AA,LLLL:XX..XX
  64.                     AA..AA is address,
  65.                     LLLL is number of bytes,
  66.                     XX..XX is data
  67.     reply        OK        for success
  68.             ENN        for an error (this includes the case
  69.                     where only part of the data was
  70.                     written).
  71.  
  72.     cont        cAA..AA        AA..AA is address to resume
  73.                     If AA..AA is omitted,
  74.                     resume at same address.
  75.  
  76.     step        sAA..AA        AA..AA is address to resume
  77.                     If AA..AA is omitted,
  78.                     resume at same address.
  79.  
  80.     last signal     ?               Reply the current reason for stopping.
  81.                                         This is the same reply as is generated
  82.                     for step or cont : SAA where AA is the
  83.                     signal number.
  84.  
  85.     There is no immediate reply to step or cont.
  86.     The reply comes when the machine stops.
  87.     It is        SAA        AA is the "signal number"
  88.  
  89.     or...        TAAn...:r...;n:r...;n...:r...;
  90.                     AA = signal number
  91.                     n... = register number
  92.                     r... = register contents
  93.     or...        WAA        The process extited, and AA is
  94.                     the exit status.  This is only
  95.                     applicable for certains sorts of
  96.                     targets.
  97.     or...        NAATT;DD;BB    Relocate the object file.
  98.                     AA = signal number
  99.                     TT = text address
  100.                     DD = data address
  101.                     BB = bss address
  102.                     This is used by the NLM stub,
  103.                     which is why it only has three
  104.                     addresses rather than one per
  105.                     section: the NLM stub always
  106.                     sees only three sections, even
  107.                     though gdb may see more.
  108.  
  109.     kill request    k
  110.  
  111.     toggle debug    d        toggle debug flag (see 386 & 68k stubs)
  112.     reset        r        reset -- see sparc stub.
  113.     reserved    <other>        On other requests, the stub should
  114.                     ignore the request and send an empty
  115.                     response ($#<checksum>).  This way
  116.                     we can extend the protocol and GDB
  117.                     can tell whether the stub it is
  118.                     talking to uses the old or the new.
  119.     search        tAA:PP,MM    Search backword starting at address
  120.                     AA for a match with pattern PP and
  121.                     mask MM.  PP and MM are 4 bytes.
  122.                     Not supported by all stubs.  */
  123.  
  124. #include "defs.h"
  125. #include <string.h>
  126. #include <fcntl.h>
  127. #include "frame.h"
  128. #include "inferior.h"
  129. #include "bfd.h"
  130. #include "symfile.h"
  131. #include "target.h"
  132. #include "wait.h"
  133. #include "terminal.h"
  134. #include "gdbcmd.h"
  135. #include "objfiles.h"
  136. #include "gdb-stabs.h"
  137.  
  138. #include "dcache.h"
  139.  
  140. #if !defined(DONT_USE_REMOTE)
  141. #ifdef USG
  142. #include <sys/types.h>
  143. #endif
  144.  
  145. #include <signal.h>
  146. #include "serial.h"
  147.  
  148. /* Prototypes for local functions */
  149.  
  150. static int
  151. remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
  152.  
  153. static int
  154. remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
  155.  
  156. static void
  157. remote_files_info PARAMS ((struct target_ops *ignore));
  158.  
  159. static int
  160. remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
  161.                 int should_write, struct target_ops *target));
  162.  
  163. static void 
  164. remote_prepare_to_store PARAMS ((void));
  165.  
  166. static void
  167. remote_fetch_registers PARAMS ((int regno));
  168.  
  169. static void
  170. remote_resume PARAMS ((int pid, int step, enum target_signal siggnal));
  171.  
  172. static int
  173. remote_start_remote PARAMS ((char *dummy));
  174.  
  175. static void
  176. remote_open PARAMS ((char *name, int from_tty));
  177.  
  178. static void
  179. remote_close PARAMS ((int quitting));
  180.  
  181. static void
  182. remote_store_registers PARAMS ((int regno));
  183.  
  184. static void
  185. getpkt PARAMS ((char *buf, int forever));
  186.  
  187. static void
  188. putpkt PARAMS ((char *buf));
  189.  
  190. static void
  191. remote_send PARAMS ((char *buf));
  192.  
  193. static int
  194. readchar PARAMS ((void));
  195.  
  196. static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
  197.  
  198. static int
  199. tohex PARAMS ((int nib));
  200.  
  201. static int
  202. fromhex PARAMS ((int a));
  203.  
  204. static void
  205. remote_detach PARAMS ((char *args, int from_tty));
  206.  
  207. static void
  208. remote_interrupt PARAMS ((int signo));
  209.  
  210. static void
  211. remote_interrupt_twice PARAMS ((int signo));
  212.  
  213. static void
  214. interrupt_query PARAMS ((void));
  215.  
  216. extern struct target_ops remote_ops;    /* Forward decl */
  217.  
  218. /* This was 5 seconds, which is a long time to sit and wait.
  219.    Unless this is going though some terminal server or multiplexer or
  220.    other form of hairy serial connection, I would think 2 seconds would
  221.    be plenty.  */
  222. static int timeout = 2;
  223.  
  224. #if 0
  225. int icache;
  226. #endif
  227.  
  228. /* Descriptor for I/O to remote machine.  Initialize it to NULL so that
  229.    remote_open knows that we don't have a file open when the program
  230.    starts.  */
  231. serial_t remote_desc = NULL;
  232.  
  233. /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
  234.    and i386-stub.c.  Normally, no one would notice because it only matters
  235.    for writing large chunks of memory (e.g. in downloads).  Also, this needs
  236.    to be more than 400 if required to hold the registers (see below, where
  237.    we round it up based on REGISTER_BYTES).  */
  238. #define    PBUFSIZ    400
  239.  
  240. /* Maximum number of bytes to read/write at once.  The value here
  241.    is chosen to fill up a packet (the headers account for the 32).  */
  242. #define MAXBUFBYTES ((PBUFSIZ-32)/2)
  243.  
  244. /* Round up PBUFSIZ to hold all the registers, at least.  */
  245. #if REGISTER_BYTES > MAXBUFBYTES
  246. #undef    PBUFSIZ
  247. #define    PBUFSIZ    (REGISTER_BYTES * 2 + 32)
  248. #endif
  249.  
  250. /* Clean up connection to a remote debugger.  */
  251.  
  252. /* ARGSUSED */
  253. static void
  254. remote_close (quitting)
  255.      int quitting;
  256. {
  257.   if (remote_desc)
  258.     SERIAL_CLOSE (remote_desc);
  259.   remote_desc = NULL;
  260. }
  261.  
  262. /* Stub for catch_errors.  */
  263.  
  264. static int
  265. remote_start_remote (dummy)
  266.      char *dummy;
  267. {
  268.   immediate_quit = 1;        /* Allow user to interrupt it */
  269.  
  270.   /* Ack any packet which the remote side has already sent.  */
  271.   /* I'm not sure this \r is needed; we don't use it any other time we
  272.      send an ack.  */
  273.   SERIAL_WRITE (remote_desc, "+\r", 2);
  274.   putpkt ("?");            /* initiate a query from remote machine */
  275.   immediate_quit = 0;
  276.  
  277.   start_remote ();        /* Initialize gdb process mechanisms */
  278.   return 1;
  279. }
  280.  
  281. /* Open a connection to a remote debugger.
  282.    NAME is the filename used for communication.  */
  283.  
  284. static DCACHE *remote_dcache;
  285.  
  286. static void
  287. remote_open (name, from_tty)
  288.      char *name;
  289.      int from_tty;
  290. {
  291.   if (name == 0)
  292.     error (
  293. "To open a remote debug connection, you need to specify what serial\n\
  294. device is attached to the remote system (e.g. /dev/ttya).");
  295.  
  296.   target_preopen (from_tty);
  297.  
  298.   unpush_target (&remote_ops);
  299.  
  300.   remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
  301.  
  302.   remote_desc = SERIAL_OPEN (name);
  303.   if (!remote_desc)
  304.     perror_with_name (name);
  305.  
  306.   if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
  307.     {
  308.       SERIAL_CLOSE (remote_desc);
  309.       perror_with_name (name);
  310.     }
  311.  
  312.   SERIAL_RAW (remote_desc);
  313.  
  314.   /* If there is something sitting in the buffer we might take it as a
  315.      response to a command, which would be bad.  */
  316.   SERIAL_FLUSH_INPUT (remote_desc);
  317.  
  318.   if (from_tty)
  319.     {
  320.       puts_filtered ("Remote debugging using ");
  321.       puts_filtered (name);
  322.       puts_filtered ("\n");
  323.     }
  324.   push_target (&remote_ops);    /* Switch to using remote target now */
  325.  
  326.   /* Start the remote connection; if error (0), discard this target.
  327.      In particular, if the user quits, be sure to discard it
  328.      (we'd be in an inconsistent state otherwise).  */
  329.   if (!catch_errors (remote_start_remote, (char *)0, 
  330.     "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
  331.     pop_target();
  332. }
  333.  
  334. /* remote_detach()
  335.    takes a program previously attached to and detaches it.
  336.    We better not have left any breakpoints
  337.    in the program or it'll die when it hits one.
  338.    Close the open connection to the remote debugger.
  339.    Use this when you want to detach and do something else
  340.    with your gdb.  */
  341.  
  342. static void
  343. remote_detach (args, from_tty)
  344.      char *args;
  345.      int from_tty;
  346. {
  347.   if (args)
  348.     error ("Argument given to \"detach\" when remotely debugging.");
  349.   
  350.   pop_target ();
  351.   if (from_tty)
  352.     puts_filtered ("Ending remote debugging.\n");
  353. }
  354.  
  355. /* Convert hex digit A to a number.  */
  356.  
  357. static int
  358. fromhex (a)
  359.      int a;
  360. {
  361.   if (a >= '0' && a <= '9')
  362.     return a - '0';
  363.   else if (a >= 'a' && a <= 'f')
  364.     return a - 'a' + 10;
  365.   else
  366.     error ("Reply contains invalid hex digit");
  367.   return -1;
  368. }
  369.  
  370. /* Convert number NIB to a hex digit.  */
  371.  
  372. static int
  373. tohex (nib)
  374.      int nib;
  375. {
  376.   if (nib < 10)
  377.     return '0'+nib;
  378.   else
  379.     return 'a'+nib-10;
  380. }
  381.  
  382. /* Tell the remote machine to resume.  */
  383.  
  384. static void
  385. remote_resume (pid, step, siggnal)
  386.      int pid, step;
  387.      enum target_signal siggnal;
  388. {
  389.   char buf[PBUFSIZ];
  390.  
  391.   if (siggnal)
  392.     {
  393.       char *name;
  394.       target_terminal_ours_for_output ();
  395.       printf_filtered
  396.     ("Can't send signals to a remote system.  %s not sent.\n",
  397.      target_signal_to_name (siggnal));
  398.       target_terminal_inferior ();
  399.     }
  400.  
  401.   dcache_flush (remote_dcache);
  402.  
  403.   strcpy (buf, step ? "s": "c");
  404.  
  405.   putpkt (buf);
  406. }
  407.  
  408. /* Send ^C to target to halt it.  Target will respond, and send us a
  409.    packet.  */
  410.  
  411. static void
  412. remote_interrupt (signo)
  413.      int signo;
  414. {
  415.   /* If this doesn't work, try more severe steps.  */
  416.   signal (signo, remote_interrupt_twice);
  417.   
  418.   if (remote_debug)
  419.     printf_unfiltered ("remote_interrupt called\n");
  420.  
  421.   SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
  422. }
  423.  
  424. static void (*ofunc)();
  425.  
  426. /* The user typed ^C twice.  */
  427. static void
  428. remote_interrupt_twice (signo)
  429.      int signo;
  430. {
  431.   signal (signo, ofunc);
  432.   
  433.   interrupt_query ();
  434.  
  435.   signal (signo, remote_interrupt);
  436. }
  437.  
  438. /* Ask the user what to do when an interrupt is received.  */
  439.  
  440. static void
  441. interrupt_query ()
  442. {
  443.   target_terminal_ours ();
  444.  
  445.   if (query ("Interrupted while waiting for the program.\n\
  446. Give up (and stop debugging it)? "))
  447.     {
  448.       target_mourn_inferior ();
  449.       return_to_top_level (RETURN_QUIT);
  450.     }
  451.  
  452.   target_terminal_inferior ();
  453. }
  454.  
  455. /* Wait until the remote machine stops, then return,
  456.    storing status in STATUS just as `wait' would.
  457.    Returns "pid" (though it's not clear what, if anything, that
  458.    means in the case of this target).  */
  459.  
  460. static int
  461. remote_wait (pid, status)
  462.      int pid;
  463.      struct target_waitstatus *status;
  464. {
  465.   unsigned char buf[PBUFSIZ];
  466.  
  467.   status->kind = TARGET_WAITKIND_EXITED;
  468.   status->value.integer = 0;
  469.  
  470.   while (1)
  471.     {
  472.       unsigned char *p;
  473.  
  474.       ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
  475.       getpkt ((char *) buf, 1);
  476.       signal (SIGINT, ofunc);
  477.  
  478.       if (buf[0] == 'E')
  479.     warning ("Remote failure reply: %s", buf);
  480.       else if (buf[0] == 'T')
  481.     {
  482.       int i;
  483.       long regno;
  484.       char regs[MAX_REGISTER_RAW_SIZE];
  485.  
  486.       /* Expedited reply, containing Signal, {regno, reg} repeat */
  487.       /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
  488.           ss = signal number
  489.           n... = register number
  490.           r... = register contents
  491.           */
  492.  
  493.       p = &buf[3];        /* after Txx */
  494.  
  495.       while (*p)
  496.         {
  497.           unsigned char *p1;
  498.  
  499.           regno = strtol (p, &p1, 16); /* Read the register number */
  500.  
  501.           if (p1 == p)
  502.         warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
  503.              p1, buf);
  504.  
  505.           p = p1;
  506.  
  507.           if (*p++ != ':')
  508.         warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
  509.              p, buf);
  510.  
  511.           if (regno >= NUM_REGS)
  512.         warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
  513.              regno, p, buf);
  514.  
  515.           for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
  516.         {
  517.           if (p[0] == 0 || p[1] == 0)
  518.             warning ("Remote reply is too short: %s", buf);
  519.           regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  520.           p += 2;
  521.         }
  522.  
  523.           if (*p++ != ';')
  524.         warning ("Remote register badly formatted: %s", buf);
  525.  
  526.           supply_register (regno, regs);
  527.         }
  528.       break;
  529.     }
  530.       else if (buf[0] == 'N')
  531.     {
  532.       unsigned char *p1;
  533.       bfd_vma text_addr, data_addr, bss_addr;
  534.  
  535.       /* Relocate object file.  Format is NAATT;DD;BB where AA is
  536.          the signal number, TT is the new text address, DD is the
  537.          new data address, and BB is the new bss address.  This is
  538.          used by the NLM stub; gdb may see more sections.  */
  539.       p = &buf[3];
  540.       text_addr = strtoul (p, &p1, 16);
  541.       if (p1 == p || *p1 != ';')
  542.         warning ("Malformed relocation packet: Packet '%s'", buf);
  543.       p = p1 + 1;
  544.       data_addr = strtoul (p, &p1, 16);
  545.       if (p1 == p || *p1 != ';')
  546.         warning ("Malformed relocation packet: Packet '%s'", buf);
  547.       p = p1 + 1;
  548.       bss_addr = strtoul (p, &p1, 16);
  549.       if (p1 == p)
  550.         warning ("Malformed relocation packet: Packet '%s'", buf);
  551.  
  552.       if (symfile_objfile != NULL
  553.           && (ANOFFSET (symfile_objfile->section_offsets,
  554.                 SECT_OFF_TEXT) != text_addr
  555.           || ANOFFSET (symfile_objfile->section_offsets,
  556.                    SECT_OFF_DATA) != data_addr
  557.           || ANOFFSET (symfile_objfile->section_offsets,
  558.                    SECT_OFF_BSS) != bss_addr))
  559.         {
  560.           struct section_offsets *offs;
  561.  
  562.           /* FIXME: This code assumes gdb-stabs.h is being used;
  563.          it's broken for xcoff, dwarf, sdb-coff, etc.  But
  564.          there is no simple canonical representation for this
  565.          stuff.  (Just what does "text" as seen by the stub
  566.          mean, anyway?).  */
  567.  
  568.           offs = ((struct section_offsets *)
  569.               alloca (sizeof (struct section_offsets)
  570.                   + (symfile_objfile->num_sections
  571.                  * sizeof (offs->offsets))));
  572.           memcpy (offs, symfile_objfile->section_offsets,
  573.               (sizeof (struct section_offsets)
  574.                + (symfile_objfile->num_sections
  575.               * sizeof (offs->offsets))));
  576.           ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
  577.           ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
  578.           ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
  579.  
  580.           objfile_relocate (symfile_objfile, offs);
  581.           {
  582.         struct obj_section *s;
  583.         bfd *abfd;
  584.  
  585.         abfd = symfile_objfile->obfd;
  586.  
  587.         for (s = symfile_objfile->sections;
  588.              s < symfile_objfile->sections_end; ++s)
  589.           {
  590.             flagword flags;
  591.  
  592.             flags = bfd_get_section_flags (abfd, s->sec_ptr);
  593.  
  594.             if (flags & SEC_CODE)
  595.               {
  596.             s->addr += text_addr;
  597.             s->endaddr += text_addr;
  598.               }
  599.             else if (flags & (SEC_DATA | SEC_LOAD))
  600.               {
  601.             s->addr += data_addr;
  602.             s->endaddr += data_addr;
  603.               }
  604.             else if (flags & SEC_ALLOC)
  605.               {
  606.             s->addr += bss_addr;
  607.             s->endaddr += bss_addr;
  608.               }
  609.           }
  610.           }
  611.         }
  612.       break;
  613.     }
  614.       else if (buf[0] == 'W')
  615.     {
  616.       /* The remote process exited.  */
  617.       status->kind = TARGET_WAITKIND_EXITED;
  618.       status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
  619.       return 0;
  620.     }
  621.       else if (buf[0] == 'S')
  622.     break;
  623.       else
  624.     warning ("Invalid remote reply: %s", buf);
  625.     }
  626.  
  627.   status->kind = TARGET_WAITKIND_STOPPED;
  628.   status->value.sig = (enum target_signal)
  629.     (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
  630.  
  631.   return 0;
  632. }
  633.  
  634. /* Number of bytes of registers this stub implements.  */
  635. static int register_bytes_found;
  636.  
  637. /* Read the remote registers into the block REGS.  */
  638. /* Currently we just read all the registers, so we don't use regno.  */
  639. /* ARGSUSED */
  640. static void
  641. remote_fetch_registers (regno)
  642.      int regno;
  643. {
  644.   char buf[PBUFSIZ];
  645.   int i;
  646.   char *p;
  647.   char regs[REGISTER_BYTES];
  648.  
  649.   sprintf (buf, "g");
  650.   remote_send (buf);
  651.  
  652.   /* Unimplemented registers read as all bits zero.  */
  653.   memset (regs, 0, REGISTER_BYTES);
  654.  
  655.   /* We can get out of synch in various cases.  If the first character
  656.      in the buffer is not a hex character, assume that has happened
  657.      and try to fetch another packet to read.  */
  658.   while ((buf[0] < '0' || buf[0] > '9')
  659.      && (buf[0] < 'a' || buf[0] > 'f'))
  660.     {
  661.       if (remote_debug)
  662.     printf_unfiltered ("Bad register packet; fetching a new packet\n");
  663.       getpkt (buf, 0);
  664.     }
  665.  
  666.   /* Reply describes registers byte by byte, each byte encoded as two
  667.      hex characters.  Suck them all up, then supply them to the
  668.      register cacheing/storage mechanism.  */
  669.  
  670.   p = buf;
  671.   for (i = 0; i < REGISTER_BYTES; i++)
  672.     {
  673.       if (p[0] == 0)
  674.     break;
  675.       if (p[1] == 0)
  676.     {
  677.       warning ("Remote reply is of odd length: %s", buf);
  678.       /* Don't change register_bytes_found in this case, and don't
  679.          print a second warning.  */
  680.       goto supply_them;
  681.     }
  682.       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  683.       p += 2;
  684.     }
  685.  
  686.   if (i != register_bytes_found)
  687.     {
  688.       register_bytes_found = i;
  689. #ifdef REGISTER_BYTES_OK
  690.       if (!REGISTER_BYTES_OK (i))
  691.     warning ("Remote reply is too short: %s", buf);
  692. #endif
  693.     }
  694.  
  695.  supply_them:
  696.   for (i = 0; i < NUM_REGS; i++)
  697.     supply_register (i, ®s[REGISTER_BYTE(i)]);
  698. }
  699.  
  700. /* Prepare to store registers.  Since we send them all, we have to
  701.    read out the ones we don't want to change first.  */
  702.  
  703. static void 
  704. remote_prepare_to_store ()
  705. {
  706.   /* Make sure the entire registers array is valid.  */
  707.   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
  708. }
  709.  
  710. /* Store the remote registers from the contents of the block REGISTERS. 
  711.    FIXME, eventually just store one register if that's all that is needed.  */
  712.  
  713. /* ARGSUSED */
  714. static void
  715. remote_store_registers (regno)
  716.      int regno;
  717. {
  718.   char buf[PBUFSIZ];
  719.   int i;
  720.   char *p;
  721.  
  722.   buf[0] = 'G';
  723.   
  724.   /* Command describes registers byte by byte,
  725.      each byte encoded as two hex characters.  */
  726.  
  727.   p = buf + 1;
  728.   /* remote_prepare_to_store insures that register_bytes_found gets set.  */
  729.   for (i = 0; i < register_bytes_found; i++)
  730.     {
  731.       *p++ = tohex ((registers[i] >> 4) & 0xf);
  732.       *p++ = tohex (registers[i] & 0xf);
  733.     }
  734.   *p = '\0';
  735.  
  736.   remote_send (buf);
  737. }
  738.  
  739. #if 0
  740.  
  741. /* Use of the data cache is disabled because it loses for looking at
  742.    and changing hardware I/O ports and the like.  Accepting `volatile'
  743.    would perhaps be one way to fix it, but a better way which would
  744.    win for more cases would be to use the executable file for the text
  745.    segment, like the `icache' code below but done cleanly (in some
  746.    target-independent place, perhaps in target_xfer_memory, perhaps
  747.    based on assigning each target a speed or perhaps by some simpler
  748.    mechanism).  */
  749.  
  750. /* Read a word from remote address ADDR and return it.
  751.    This goes through the data cache.  */
  752.  
  753. static int
  754. remote_fetch_word (addr)
  755.      CORE_ADDR addr;
  756. {
  757. #if 0
  758.   if (icache)
  759.     {
  760.       extern CORE_ADDR text_start, text_end;
  761.  
  762.       if (addr >= text_start && addr < text_end)
  763.     {
  764.       int buffer;
  765.       xfer_core_file (addr, &buffer, sizeof (int));
  766.       return buffer;
  767.     }
  768.     }
  769. #endif
  770.   return dcache_fetch (remote_dcache, addr);
  771. }
  772.  
  773. /* Write a word WORD into remote address ADDR.
  774.    This goes through the data cache.  */
  775.  
  776. static void
  777. remote_store_word (addr, word)
  778.      CORE_ADDR addr;
  779.      int word;
  780. {
  781.   dcache_poke (remote_dcache, addr, word);
  782. }
  783. #endif /* 0 */
  784.  
  785. /* Write memory data directly to the remote machine.
  786.    This does not inform the data cache; the data cache uses this.
  787.    MEMADDR is the address in the remote memory space.
  788.    MYADDR is the address of the buffer in our space.
  789.    LEN is the number of bytes.
  790.  
  791.    Returns number of bytes transferred, or 0 for error.  */
  792.  
  793. static int
  794. remote_write_bytes (memaddr, myaddr, len)
  795.      CORE_ADDR memaddr;
  796.      unsigned char *myaddr;
  797.      int len;
  798. {
  799.   char buf[PBUFSIZ];
  800.   int i;
  801.   char *p;
  802.  
  803.   sprintf (buf, "M%x,%x:", memaddr, len);
  804.  
  805.   /* We send target system values byte by byte, in increasing byte addresses,
  806.      each byte encoded as two hex characters.  */
  807.  
  808.   p = buf + strlen (buf);
  809.   for (i = 0; i < len; i++)
  810.     {
  811.       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
  812.       *p++ = tohex (myaddr[i] & 0xf);
  813.     }
  814.   *p = '\0';
  815.  
  816.   putpkt (buf);
  817.   getpkt (buf, 0);
  818.  
  819.   if (buf[0] == 'E')
  820.     {
  821.       /* There is no correspondance between what the remote protocol uses
  822.      for errors and errno codes.  We would like a cleaner way of
  823.      representing errors (big enough to include errno codes, bfd_error
  824.      codes, and others).  But for now just return EIO.  */
  825.       errno = EIO;
  826.       return 0;
  827.     }
  828.   return len;
  829. }
  830.  
  831. /* Read memory data directly from the remote machine.
  832.    This does not use the data cache; the data cache uses this.
  833.    MEMADDR is the address in the remote memory space.
  834.    MYADDR is the address of the buffer in our space.
  835.    LEN is the number of bytes.
  836.  
  837.    Returns number of bytes transferred, or 0 for error.  */
  838.  
  839. static int
  840. remote_read_bytes (memaddr, myaddr, len)
  841.      CORE_ADDR memaddr;
  842.      unsigned char *myaddr;
  843.      int len;
  844. {
  845.   char buf[PBUFSIZ];
  846.   int i;
  847.   char *p;
  848.  
  849.   if (len > PBUFSIZ / 2 - 1)
  850.     abort ();
  851.  
  852.   sprintf (buf, "m%x,%x", memaddr, len);
  853.   putpkt (buf);
  854.   getpkt (buf, 0);
  855.  
  856.   if (buf[0] == 'E')
  857.     {
  858.       /* There is no correspondance between what the remote protocol uses
  859.      for errors and errno codes.  We would like a cleaner way of
  860.      representing errors (big enough to include errno codes, bfd_error
  861.      codes, and others).  But for now just return EIO.  */
  862.       errno = EIO;
  863.       return 0;
  864.     }
  865.  
  866.   /* Reply describes memory byte by byte,
  867.      each byte encoded as two hex characters.  */
  868.  
  869.   p = buf;
  870.   for (i = 0; i < len; i++)
  871.     {
  872.       if (p[0] == 0 || p[1] == 0)
  873.     /* Reply is short.  This means that we were able to read only part
  874.        of what we wanted to.  */
  875.     break;
  876.       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  877.       p += 2;
  878.     }
  879.   return i;
  880. }
  881.  
  882. /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
  883.    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
  884.    nonzero.  Returns length of data written or read; 0 for error.  */
  885.  
  886. /* ARGSUSED */
  887. static int
  888. remote_xfer_memory(memaddr, myaddr, len, should_write, target)
  889.      CORE_ADDR memaddr;
  890.      char *myaddr;
  891.      int len;
  892.      int should_write;
  893.      struct target_ops *target;            /* ignored */
  894. {
  895.   int xfersize;
  896.   int bytes_xferred;
  897.   int total_xferred = 0;
  898.  
  899.   while (len > 0)
  900.     {
  901.       if (len > MAXBUFBYTES)
  902.     xfersize = MAXBUFBYTES;
  903.       else
  904.     xfersize = len;
  905.  
  906.       if (should_write)
  907.         bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
  908.       else
  909.     bytes_xferred = remote_read_bytes (memaddr, myaddr, xfersize);
  910.  
  911.       /* If we get an error, we are done xferring.  */
  912.       if (bytes_xferred == 0)
  913.     break;
  914.  
  915.       memaddr += bytes_xferred;
  916.       myaddr  += bytes_xferred;
  917.       len     -= bytes_xferred;
  918.       total_xferred += bytes_xferred;
  919.     }
  920.   return total_xferred;
  921. }
  922.  
  923. #if 0
  924. /* Enable after 4.12.  */
  925.  
  926. void
  927. remote_search (len, data, mask, startaddr, increment, lorange, hirange
  928.            addr_found, data_found)
  929.      int len;
  930.      char *data;
  931.      char *mask;
  932.      CORE_ADDR startaddr;
  933.      int increment;
  934.      CORE_ADDR lorange;
  935.      CORE_ADDR hirange;
  936.      CORE_ADDR *addr_found;
  937.      char *data_found;
  938. {
  939.   if (increment == -4 && len == 4)
  940.     {
  941.       long mask_long, data_long;
  942.       long data_found_long;
  943.       CORE_ADDR addr_we_found;
  944.       char buf[PBUFSIZ];
  945.       long returned_long[2];
  946.       char *p;
  947.  
  948.       mask_long = extract_unsigned_integer (mask, len);
  949.       data_long = extract_unsigned_integer (data, len);
  950.       sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
  951.       putpkt (buf);
  952.       getpkt (buf, 0);
  953.       if (buf[0] == '\0')
  954.     {
  955.       /* The stub doesn't support the 't' request.  We might want to
  956.          remember this fact, but on the other hand the stub could be
  957.          switched on us.  Maybe we should remember it only until
  958.          the next "target remote".  */
  959.       generic_search (len, data, mask, startaddr, increment, lorange,
  960.               hirange, addr_found, data_found);
  961.       return;
  962.     }
  963.  
  964.       if (buf[0] == 'E')
  965.     /* There is no correspondance between what the remote protocol uses
  966.        for errors and errno codes.  We would like a cleaner way of
  967.        representing errors (big enough to include errno codes, bfd_error
  968.        codes, and others).  But for now just use EIO.  */
  969.     memory_error (EIO, startaddr);
  970.       p = buf;
  971.       addr_we_found = 0;
  972.       while (*p != '\0' && *p != ',')
  973.     addr_we_found = (addr_we_found << 4) + fromhex (*p++);
  974.       if (*p == '\0')
  975.     error ("Protocol error: short return for search");
  976.  
  977.       data_found_long = 0;
  978.       while (*p != '\0' && *p != ',')
  979.     data_found_long = (data_found_long << 4) + fromhex (*p++);
  980.       /* Ignore anything after this comma, for future extensions.  */
  981.  
  982.       if (addr_we_found < lorange || addr_we_found >= hirange)
  983.     {
  984.       *addr_found = 0;
  985.       return;
  986.     }
  987.  
  988.       *addr_found = addr_we_found;
  989.       *data_found = store_unsigned_integer (data_we_found, len);
  990.       return;
  991.     }
  992.   generic_search (len, data, mask, startaddr, increment, lorange,
  993.           hirange, addr_found, data_found);
  994. }
  995. #endif /* 0 */
  996.  
  997. static void
  998. remote_files_info (ignore)
  999.      struct target_ops *ignore;
  1000. {
  1001.   puts_filtered ("Debugging a target over a serial line.\n");
  1002. }
  1003.  
  1004. /* Stuff for dealing with the packets which are part of this protocol.
  1005.    See comment at top of file for details.  */
  1006.  
  1007. /* Read a single character from the remote end, masking it down to 7 bits. */
  1008.  
  1009. static int
  1010. readchar ()
  1011. {
  1012.   int ch;
  1013.  
  1014.   ch = SERIAL_READCHAR (remote_desc, timeout);
  1015.  
  1016.   if (ch < 0)
  1017.     return ch;
  1018.  
  1019.   return ch & 0x7f;
  1020. }
  1021.  
  1022. /* Send the command in BUF to the remote machine,
  1023.    and read the reply into BUF.
  1024.    Report an error if we get an error reply.  */
  1025.  
  1026. static void
  1027. remote_send (buf)
  1028.      char *buf;
  1029. {
  1030.  
  1031.   putpkt (buf);
  1032.   getpkt (buf, 0);
  1033.  
  1034.   if (buf[0] == 'E')
  1035.     error ("Remote failure reply: %s", buf);
  1036. }
  1037.  
  1038. /* Send a packet to the remote machine, with error checking.
  1039.    The data of the packet is in BUF.  */
  1040.  
  1041. static void
  1042. putpkt (buf)
  1043.      char *buf;
  1044. {
  1045.   int i;
  1046.   unsigned char csum = 0;
  1047.   char buf2[PBUFSIZ];
  1048.   int cnt = strlen (buf);
  1049.   int ch;
  1050.   char *p;
  1051.  
  1052.   /* Copy the packet into buffer BUF2, encapsulating it
  1053.      and giving it a checksum.  */
  1054.  
  1055.   if (cnt > sizeof(buf2) - 5)        /* Prosanity check */
  1056.     abort();
  1057.  
  1058.   p = buf2;
  1059.   *p++ = '$';
  1060.  
  1061.   for (i = 0; i < cnt; i++)
  1062.     {
  1063.       csum += buf[i];
  1064.       *p++ = buf[i];
  1065.     }
  1066.   *p++ = '#';
  1067.   *p++ = tohex ((csum >> 4) & 0xf);
  1068.   *p++ = tohex (csum & 0xf);
  1069.  
  1070.   /* Send it over and over until we get a positive ack.  */
  1071.  
  1072.   while (1)
  1073.     {
  1074.       if (remote_debug)
  1075.     {
  1076.       *p = '\0';
  1077.       printf_unfiltered ("Sending packet: %s...", buf2);  gdb_flush(gdb_stdout);
  1078.     }
  1079.       if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
  1080.     perror_with_name ("putpkt: write failed");
  1081.  
  1082.       /* read until either a timeout occurs (-2) or '+' is read */
  1083.       while (1)
  1084.     {
  1085.       ch = readchar ();
  1086.  
  1087.       switch (ch)
  1088.         {
  1089.         case '+':
  1090.           if (remote_debug)
  1091.         printf_unfiltered("Ack\n");
  1092.           return;
  1093.         case SERIAL_TIMEOUT:
  1094.           break;        /* Retransmit buffer */
  1095.         case SERIAL_ERROR:
  1096.           perror_with_name ("putpkt: couldn't read ACK");
  1097.         case SERIAL_EOF:
  1098.           error ("putpkt: EOF while trying to read ACK");
  1099.         default:
  1100.           if (remote_debug)
  1101.         printf_unfiltered ("%02X %c ", ch&0xFF, ch);
  1102.           continue;
  1103.         }
  1104.       break;        /* Here to retransmit */
  1105.     }
  1106.  
  1107. #if 0
  1108.       /* This is wrong.  If doing a long backtrace, the user should be
  1109.      able to get out next time we call QUIT, without anything as violent
  1110.      as interrupt_query.  If we want to provide a way out of here
  1111.      without getting to the next QUIT, it should be based on hitting
  1112.      ^C twice as in remote_wait.  */
  1113.       if (quit_flag)
  1114.     {
  1115.       quit_flag = 0;
  1116.       interrupt_query ();
  1117.     }
  1118. #endif
  1119.     }
  1120. }
  1121.  
  1122. /* Read a packet from the remote machine, with error checking,
  1123.    and store it in BUF.  BUF is expected to be of size PBUFSIZ.
  1124.    If FOREVER, wait forever rather than timing out; this is used
  1125.    while the target is executing user code.  */
  1126.  
  1127. static void
  1128. getpkt (buf, forever)
  1129.      char *buf;
  1130.      int forever;
  1131. {
  1132.   char *bp;
  1133.   unsigned char csum;
  1134.   int c = 0;
  1135.   unsigned char c1, c2;
  1136.   int retries = 0;
  1137. #define MAX_RETRIES    10
  1138.  
  1139.   while (1)
  1140.     {
  1141. #if 0
  1142.       /* This is wrong.  If doing a long backtrace, the user should be
  1143.      able to get out time next we call QUIT, without anything as violent
  1144.      as interrupt_query.  If we want to provide a way out of here
  1145.      without getting to the next QUIT, it should be based on hitting
  1146.      ^C twice as in remote_wait.  */
  1147.       if (quit_flag)
  1148.     {
  1149.       quit_flag = 0;
  1150.       interrupt_query ();
  1151.     }
  1152. #endif
  1153.  
  1154.       /* This can loop forever if the remote side sends us characters
  1155.      continuously, but if it pauses, we'll get a zero from readchar
  1156.      because of timeout.  Then we'll count that as a retry.  */
  1157.  
  1158.       c = readchar();
  1159.       if (c > 0 && c != '$')
  1160.     continue;
  1161.  
  1162.       if (c == SERIAL_TIMEOUT)
  1163.     {
  1164.       if (forever)
  1165.         continue;
  1166.       if (++retries >= MAX_RETRIES)
  1167.         if (remote_debug) puts_filtered ("Timed out.\n");
  1168.       goto out;
  1169.     }
  1170.  
  1171.       if (c == SERIAL_EOF)
  1172.     error ("Remote connection closed");
  1173.       if (c == SERIAL_ERROR)
  1174.     perror_with_name ("Remote communication error");
  1175.  
  1176.       /* Force csum to be zero here because of possible error retry.  */
  1177.       csum = 0;
  1178.       bp = buf;
  1179.  
  1180.       while (1)
  1181.     {
  1182.       c = readchar ();
  1183.       if (c == SERIAL_TIMEOUT)
  1184.         {
  1185.           if (remote_debug)
  1186.         puts_filtered ("Timeout in mid-packet, retrying\n");
  1187.           goto whole;        /* Start a new packet, count retries */
  1188.         } 
  1189.       if (c == '$')
  1190.         {
  1191.           if (remote_debug)
  1192.         puts_filtered ("Saw new packet start in middle of old one\n");
  1193.           goto whole;        /* Start a new packet, count retries */
  1194.         }
  1195.       if (c == '#')
  1196.         break;
  1197.       if (bp >= buf+PBUFSIZ-1)
  1198.       {
  1199.         *bp = '\0';
  1200.         puts_filtered ("Remote packet too long: ");
  1201.         puts_filtered (buf);
  1202.         puts_filtered ("\n");
  1203.         goto whole;
  1204.       }
  1205.       *bp++ = c;
  1206.       csum += c;
  1207.     }
  1208.       *bp = 0;
  1209.  
  1210.       c1 = fromhex (readchar ());
  1211.       c2 = fromhex (readchar ());
  1212.       if ((csum & 0xff) == (c1 << 4) + c2)
  1213.     break;
  1214.       printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
  1215.           (c1 << 4) + c2, csum & 0xff);
  1216.       puts_filtered (buf);
  1217.       puts_filtered ("\n");
  1218.  
  1219.       /* Try the whole thing again.  */
  1220. whole:
  1221.       if (++retries < MAX_RETRIES)
  1222.     {
  1223.       SERIAL_WRITE (remote_desc, "-", 1);
  1224.     }
  1225.       else
  1226.     {
  1227.       printf_unfiltered ("Ignoring packet error, continuing...\n");
  1228.       break;
  1229.     }
  1230.     }
  1231.  
  1232. out:
  1233.  
  1234.   SERIAL_WRITE (remote_desc, "+", 1);
  1235.  
  1236.   if (remote_debug)
  1237.     fprintf_unfiltered (gdb_stderr,"Packet received: %s\n", buf);
  1238. }
  1239.  
  1240. static void
  1241. remote_kill ()
  1242. {
  1243.   putpkt ("k");
  1244.   /* Don't wait for it to die.  I'm not really sure it matters whether
  1245.      we do or not.  For the existing stubs, kill is a noop.  */
  1246.   target_mourn_inferior ();
  1247. }
  1248.  
  1249. static void
  1250. remote_mourn ()
  1251. {
  1252.   unpush_target (&remote_ops);
  1253.   generic_mourn_inferior ();
  1254. }
  1255.  
  1256. #ifdef REMOTE_BREAKPOINT
  1257.  
  1258. /* On some machines, e.g. 68k, we may use a different breakpoint instruction
  1259.    than other targets.  */
  1260. static unsigned char break_insn[] = REMOTE_BREAKPOINT;
  1261.  
  1262. /* Check that it fits in BREAKPOINT_MAX bytes.  */
  1263. static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
  1264.  
  1265. #else /* No REMOTE_BREAKPOINT.  */
  1266.  
  1267. /* Same old breakpoint instruction.  This code does nothing different
  1268.    than mem-break.c.  */
  1269. static unsigned char break_insn[] = BREAKPOINT;
  1270.  
  1271. #endif /* No REMOTE_BREAKPOINT.  */
  1272.  
  1273. /* Insert a breakpoint on targets that don't have any better breakpoint
  1274.    support.  We read the contents of the target location and stash it,
  1275.    then overwrite it with a breakpoint instruction.  ADDR is the target
  1276.    location in the target machine.  CONTENTS_CACHE is a pointer to 
  1277.    memory allocated for saving the target contents.  It is guaranteed
  1278.    by the caller to be long enough to save sizeof BREAKPOINT bytes (this
  1279.    is accomplished via BREAKPOINT_MAX).  */
  1280.  
  1281. static int
  1282. remote_insert_breakpoint (addr, contents_cache)
  1283.      CORE_ADDR addr;
  1284.      char *contents_cache;
  1285. {
  1286.   int val;
  1287.  
  1288.   val = target_read_memory (addr, contents_cache, sizeof break_insn);
  1289.  
  1290.   if (val == 0)
  1291.     val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
  1292.  
  1293.   return val;
  1294. }
  1295.  
  1296. static int
  1297. remote_remove_breakpoint (addr, contents_cache)
  1298.      CORE_ADDR addr;
  1299.      char *contents_cache;
  1300. {
  1301.   return target_write_memory (addr, contents_cache, sizeof break_insn);
  1302. }
  1303.  
  1304. /* Define the target subroutine names */
  1305.  
  1306. struct target_ops remote_ops = {
  1307.   "remote",            /* to_shortname */
  1308.   "Remote serial target in gdb-specific protocol",    /* to_longname */
  1309.   "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
  1310. Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
  1311.   remote_open,            /* to_open */
  1312.   remote_close,            /* to_close */
  1313.   NULL,                /* to_attach */
  1314.   remote_detach,        /* to_detach */
  1315.   remote_resume,        /* to_resume */
  1316.   remote_wait,            /* to_wait */
  1317.   remote_fetch_registers,    /* to_fetch_registers */
  1318.   remote_store_registers,    /* to_store_registers */
  1319.   remote_prepare_to_store,    /* to_prepare_to_store */
  1320.   remote_xfer_memory,        /* to_xfer_memory */
  1321.   remote_files_info,        /* to_files_info */
  1322.  
  1323.   remote_insert_breakpoint,    /* to_insert_breakpoint */
  1324.   remote_remove_breakpoint,    /* to_remove_breakpoint */
  1325.  
  1326.   NULL,                /* to_terminal_init */
  1327.   NULL,                /* to_terminal_inferior */
  1328.   NULL,                /* to_terminal_ours_for_output */
  1329.   NULL,                /* to_terminal_ours */
  1330.   NULL,                /* to_terminal_info */
  1331.   remote_kill,            /* to_kill */
  1332.   generic_load,            /* to_load */
  1333.   NULL,                /* to_lookup_symbol */
  1334.   NULL,                /* to_create_inferior */
  1335.   remote_mourn,            /* to_mourn_inferior */
  1336.   0,                /* to_can_run */
  1337.   0,                /* to_notice_signals */
  1338.   process_stratum,        /* to_stratum */
  1339.   NULL,                /* to_next */
  1340.   1,                /* to_has_all_memory */
  1341.   1,                /* to_has_memory */
  1342.   1,                /* to_has_stack */
  1343.   1,                /* to_has_registers */
  1344.   1,                /* to_has_execution */
  1345.   NULL,                /* sections */
  1346.   NULL,                /* sections_end */
  1347.   OPS_MAGIC            /* to_magic */
  1348. };
  1349. #endif /* Use remote.  */
  1350.  
  1351. void
  1352. _initialize_remote ()
  1353. {
  1354. #if !defined(DONT_USE_REMOTE)
  1355.   add_target (&remote_ops);
  1356. #endif
  1357. }
  1358.